package com.homestay.service.impl;

import com.homestay.beans.Home;
import com.homestay.dao.HomeDao;
import com.homestay.dao.impl.HomeDaoImpl;
import com.homestay.exception.HomeException;
import com.homestay.service.HomeService;

import java.util.List;

public class HomeServiceImpl implements HomeService {

    HomeDao homeDao = new HomeDaoImpl();

    @Override
    public List<Home> query(String h_type, String state, String num) {
        return homeDao.query(h_type, state, num);
    }

    @Override
    public int add(Home home) throws HomeException {
        // 检查民宿房间号是否存在
        int homeNum = home.getNum();
        int count = homeDao.queryByNum(homeNum).size();
        if (count > 0) {
            throw new HomeException("此房间已存在");
        }
        return homeDao.add(home);
    }

    @Override
    public int deleteById(String id) throws HomeException {
        // 状态为已入住的房间不能删除
        Home home = homeDao.queryById(id);
        if ("已入住".equals(home.getState())) {
            throw new HomeException("此房间已入住，不能删除");
        }
        return homeDao.deleteById(id);
    }

    @Override
    public Home queryById(String id) {
        return homeDao.queryById(id);
    }

    @Override
    public int update(Home home) {
        return homeDao.update(home);
    }

    @Override
    public int updateStateById(String homeId,String state) {
        return homeDao.updateStateById(homeId,state);
    }

}
